/*   1:    */ package nl.siegmann.epublib.bookprocessor;
/*   2:    */ 
/*   3:    */ import java.awt.AlphaComposite;
/*   4:    */ import java.awt.Dimension;
/*   5:    */ import java.awt.Graphics2D;
/*   6:    */ import java.awt.Image;
/*   7:    */ import java.awt.image.BufferedImage;
/*   8:    */ import java.io.ByteArrayInputStream;
/*   9:    */ import java.io.ByteArrayOutputStream;
/*  10:    */ import java.io.IOException;
/*  11:    */ import javax.imageio.ImageIO;
/*  12:    */ import nl.siegmann.epublib.domain.Book;
/*  13:    */ import nl.siegmann.epublib.domain.Metadata;
/*  14:    */ import nl.siegmann.epublib.domain.Resource;
/*  15:    */ import nl.siegmann.epublib.domain.Resources;
/*  16:    */ import nl.siegmann.epublib.domain.Spine;
/*  17:    */ import nl.siegmann.epublib.epub.BookProcessor;
/*  18:    */ import nl.siegmann.epublib.service.MediatypeService;
/*  19:    */ import nl.siegmann.epublib.util.CollectionUtil;
/*  20:    */ import nl.siegmann.epublib.util.ResourceUtil;
/*  21:    */ import org.apache.commons.io.FilenameUtils;
/*  22:    */ import org.apache.commons.lang.StringEscapeUtils;
/*  23:    */ import org.apache.commons.lang.StringUtils;
/*  24:    */ import org.slf4j.Logger;
/*  25:    */ import org.slf4j.LoggerFactory;
/*  26:    */ import org.w3c.dom.Document;
/*  27:    */ import org.w3c.dom.Element;
/*  28:    */ import org.w3c.dom.NodeList;
/*  29:    */ 
/*  30:    */ public class CoverpageBookProcessor
/*  31:    */   implements BookProcessor
/*  32:    */ {
/*  33: 43 */   public static int MAX_COVER_IMAGE_SIZE = 999;
/*  34: 44 */   private static final Logger log = LoggerFactory.getLogger(CoverpageBookProcessor.class);
/*  35:    */   public static final String DEFAULT_COVER_PAGE_ID = "cover";
/*  36:    */   public static final String DEFAULT_COVER_PAGE_HREF = "cover.html";
/*  37:    */   public static final String DEFAULT_COVER_IMAGE_ID = "cover-image";
/*  38:    */   public static final String DEFAULT_COVER_IMAGE_HREF = "images/cover.png";
/*  39:    */   
/*  40:    */   public Book processBook(Book book)
/*  41:    */   {
/*  42: 52 */     Metadata metadata = book.getMetadata();
/*  43: 53 */     if ((book.getCoverPage() == null) && (book.getCoverImage() == null)) {
/*  44: 54 */       return book;
/*  45:    */     }
/*  46: 56 */     Resource coverPage = book.getCoverPage();
/*  47: 57 */     if (coverPage == null)
/*  48:    */     {
/*  49: 58 */       coverPage = findCoverPage(book);
/*  50: 59 */       book.setCoverPage(coverPage);
/*  51:    */     }
/*  52: 61 */     Resource coverImage = book.getCoverImage();
/*  53: 62 */     if (coverPage == null)
/*  54:    */     {
/*  55: 63 */       if (coverImage != null)
/*  56:    */       {
/*  57: 66 */         if (StringUtils.isBlank(coverImage.getHref())) {
/*  58: 67 */           coverImage.setHref(getCoverImageHref(coverImage, book));
/*  59:    */         }
/*  60: 69 */         String coverPageHtml = createCoverpageHtml((String)CollectionUtil.first(metadata.getTitles()), coverImage.getHref());
/*  61: 70 */         coverPage = new Resource(null, coverPageHtml.getBytes(), getCoverPageHref(book), MediatypeService.XHTML);
/*  62: 71 */         fixCoverResourceId(book, coverPage, "cover");
/*  63:    */       }
/*  64:    */     }
/*  65: 74 */     else if (book.getCoverImage() == null)
/*  66:    */     {
/*  67: 75 */       coverImage = getFirstImageSource(coverPage, book.getResources());
/*  68: 76 */       book.setCoverImage(coverImage);
/*  69: 77 */       if (coverImage != null) {
/*  70: 78 */         book.getResources().remove(coverImage.getHref());
/*  71:    */       }
/*  72:    */     }
/*  73: 85 */     book.setCoverImage(coverImage);
/*  74: 86 */     book.setCoverPage(coverPage);
/*  75: 87 */     setCoverResourceIds(book);
/*  76: 88 */     return book;
/*  77:    */   }
/*  78:    */   
/*  79:    */   private Resource findCoverPage(Book book)
/*  80:    */   {
/*  81: 96 */     if (book.getCoverPage() != null) {
/*  82: 97 */       return book.getCoverPage();
/*  83:    */     }
/*  84: 99 */     if (!book.getSpine().isEmpty()) {
/*  85:100 */       return book.getSpine().getResource(0);
/*  86:    */     }
/*  87:102 */     return null;
/*  88:    */   }
/*  89:    */   
/*  90:    */   private void setCoverResourceIds(Book book)
/*  91:    */   {
/*  92:106 */     if (book.getCoverImage() != null) {
/*  93:107 */       fixCoverResourceId(book, book.getCoverImage(), "cover-image");
/*  94:    */     }
/*  95:109 */     if (book.getCoverPage() != null) {
/*  96:110 */       fixCoverResourceId(book, book.getCoverPage(), "cover");
/*  97:    */     }
/*  98:    */   }
/*  99:    */   
/* 100:    */   private void fixCoverResourceId(Book book, Resource resource, String defaultId)
/* 101:    */   {
/* 102:116 */     if (StringUtils.isBlank(resource.getId())) {
/* 103:117 */       resource.setId(defaultId);
/* 104:    */     }
/* 105:119 */     book.getResources().fixResourceId(resource);
/* 106:    */   }
/* 107:    */   
/* 108:    */   private String getCoverPageHref(Book book)
/* 109:    */   {
/* 110:123 */     return "cover.html";
/* 111:    */   }
/* 112:    */   
/* 113:    */   private String getCoverImageHref(Resource imageResource, Book book)
/* 114:    */   {
/* 115:128 */     return "images/cover.png";
/* 116:    */   }
/* 117:    */   
/* 118:    */   private Resource getFirstImageSource(Resource titlePageResource, Resources resources)
/* 119:    */   {
/* 120:    */     try
/* 121:    */     {
/* 122:133 */       Document titlePageDocument = ResourceUtil.getAsDocument(titlePageResource);
/* 123:134 */       NodeList imageElements = titlePageDocument.getElementsByTagName("img");
/* 124:135 */       for (int i = 0; i < imageElements.getLength(); i++)
/* 125:    */       {
/* 126:136 */         String relativeImageHref = ((Element)imageElements.item(i)).getAttribute("src");
/* 127:137 */         String absoluteImageHref = calculateAbsoluteImageHref(relativeImageHref, titlePageResource.getHref());
/* 128:138 */         Resource imageResource = resources.getByHref(absoluteImageHref);
/* 129:139 */         if (imageResource != null) {
/* 130:140 */           return imageResource;
/* 131:    */         }
/* 132:    */       }
/* 133:    */     }
/* 134:    */     catch (Exception e)
/* 135:    */     {
/* 136:144 */       log.error(e.getMessage(), e);
/* 137:    */     }
/* 138:146 */     return null;
/* 139:    */   }
/* 140:    */   
/* 141:    */   static String calculateAbsoluteImageHref(String relativeImageHref, String baseHref)
/* 142:    */   {
/* 143:154 */     if (relativeImageHref.startsWith("/")) {
/* 144:155 */       return relativeImageHref;
/* 145:    */     }
/* 146:157 */     String result = FilenameUtils.normalize(baseHref.substring(0, baseHref.lastIndexOf('/') + 1) + relativeImageHref, true);
/* 147:158 */     return result;
/* 148:    */   }
/* 149:    */   
/* 150:    */   private String createCoverpageHtml(String title, String imageHref)
/* 151:    */   {
/* 152:162 */     return "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n\t<head>\n\t\t<title>Cover</title>\n\t\t<style type=\"text/css\"> img { max-width: 100%; } </style>\n\t</head>\n\t<body>\n\t\t<div id=\"cover-image\">\n\t\t\t<img src=\"" + StringEscapeUtils.escapeHtml(imageHref) + "\" alt=\"" + StringEscapeUtils.escapeHtml(title) + "\"/>\n" + "\t\t</div>\n" + "\t</body>\n" + "</html>\n";
/* 153:    */   }
/* 154:    */   
/* 155:    */   private Dimension calculateResizeSize(BufferedImage image)
/* 156:    */   {
/* 157:    */     Dimension result;
/* 158:    */     Dimension result;
/* 159:179 */     if (image.getWidth() > image.getHeight()) {
/* 160:180 */       result = new Dimension(MAX_COVER_IMAGE_SIZE, (int)(MAX_COVER_IMAGE_SIZE / image.getWidth() * image.getHeight()));
/* 161:    */     } else {
/* 162:182 */       result = new Dimension((int)(MAX_COVER_IMAGE_SIZE / image.getHeight() * image.getWidth()), MAX_COVER_IMAGE_SIZE);
/* 163:    */     }
/* 164:184 */     return result;
/* 165:    */   }
/* 166:    */   
/* 167:    */   private byte[] createThumbnail(byte[] imageData)
/* 168:    */     throws IOException
/* 169:    */   {
/* 170:190 */     BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(imageData));
/* 171:191 */     Dimension thumbDimension = calculateResizeSize(originalImage);
/* 172:192 */     BufferedImage thumbnailImage = createResizedCopy(originalImage, (int)thumbDimension.getWidth(), (int)thumbDimension.getHeight(), false);
/* 173:193 */     ByteArrayOutputStream result = new ByteArrayOutputStream();
/* 174:194 */     ImageIO.write(thumbnailImage, "png", result);
/* 175:195 */     return result.toByteArray();
/* 176:    */   }
/* 177:    */   
/* 178:    */   private BufferedImage createResizedCopy(Image originalImage, int scaledWidth, int scaledHeight, boolean preserveAlpha)
/* 179:    */   {
/* 180:200 */     int imageType = preserveAlpha ? 1 : 2;
/* 181:201 */     BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, imageType);
/* 182:202 */     Graphics2D g = scaledBI.createGraphics();
/* 183:203 */     if (preserveAlpha) {
/* 184:204 */       g.setComposite(AlphaComposite.Src);
/* 185:    */     }
/* 186:206 */     g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null);
/* 187:207 */     g.dispose();
/* 188:208 */     return scaledBI;
/* 189:    */   }
/* 190:    */ }


/* Location:           C:\Users\David\workspace\CosaRaraPorqueSi\main\epublib-tools-3.1.jar
 * Qualified Name:     nl.siegmann.epublib.bookprocessor.CoverpageBookProcessor
 * JD-Core Version:    0.7.0.1
 */